package rmhc.api
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.system.System;
	
	import rmhc.api.dataflow.UserInfo;
	import rmhc.api.dataflow.WallPostInfo;
	import rmhc.api.fb.FbApi;
	import rmhc.api.tasks.BaseApiTask;
	import rmhc.api.tasks.IApiTask;
	import rmhc.api.tasks.IGetFriends;
	import rmhc.api.tasks.IGetProfilePicture;
	import rmhc.api.tasks.IGetProfileThumb;
	import rmhc.api.tasks.IPrepareProfileUpload;
	import rmhc.api.tasks.ISetProfilePicture;
	import rmhc.api.tasks.IWallPost;
	import rmhc.api.vk.VkApi;
	
	/**
	 * базовый класс для АПИ врапперов (в нём можно делать какой то общий функционал если будет)
	 */
	
	public class ApiWrapper extends EventDispatcher
	{
		public static const VK:int = 1;
		public static const FB:int = 2;
		
		//static public const API_INITED:String = "apiInited";
		protected var tasks:Vector.<BaseApiTask>;
		protected var LC:LoaderContext;
		
		public function ApiWrapper()
		{
			//var dom:ApplicationDomain = new ApplicationDomain( ApplicationDomain.currentDomain );
			LC = new LoaderContext( true , ApplicationDomain.currentDomain );
			tasks = new Vector.<BaseApiTask>();
		}
		
		public static function getApi( params:Object ):ApiWrapper
		{
			var a:ApiWrapper;
			if ( params["fb"] == "1" )
			{
				//TODO: FbApi
				a = new FbApi( params );
			}
			else
			{
				/*if ( !params.viewer_id )
				{
					params['api_id'] = 2661805;
					params['viewer_id'] = 34722749;
					params['sid'] = "76ff285534b89ac6121b5f9b2af542a74ce6e35725e999c56f4e0372b3219a";
					params['secret'] = "8c494e5304";
				}*/			
				a = new VkApi( params );
			}
			return a;
		}
		
		public function connect():void
		{
			
		}
		
		public function getContext():LoaderContext
		{
			return LC;
			//trace( "context!" );
			//return new LoaderContext( true );, ApplicationDomain.currentDomain );//, SecurityDomain.currentDomain );
		}
		
		/**
		 * Подготавливает всё для загрузки фотографии на сервер
		 */
		public function prepareProfileUpload():IPrepareProfileUpload
		{
			throw(new Error("Error in ApiWrapper:prepareProfileUpload. "));
		}
		public function getProfileThumb( profileId:String ):IGetProfileThumb
		{
			throw(new Error("Error in ApiWrapper:getProfileThumb. "));
		}
		public function getProfilePicture( profileId:String ):IGetProfilePicture
		{
			throw(new Error("Error in ApiWrapper:getProfilePicture. "));
		}
		public function setProfilePicture( picture:BitmapData ):ISetProfilePicture
		{
			throw(new Error("Error in ApiWrapper:setProfilePicture. "));
		}
		public function postToWall( post:WallPostInfo ):IWallPost
		{
			throw(new Error("Error in ApiWrapper:postToWall. "));
		}
		public function getFriends():IGetFriends
		{
			throw(new Error("Error in ApiWrapper:getFriends. "));
		}
		
		public function getLadoshkId():String
		{
			return null;
		}
		public function getAppUrl():String
		{
			throw(new Error("Error in ApiWrapper:getAppUrl. "));
		}
		public function getMe():UserInfo {
			throw(new Error("Error in ApiWrapper:getMe."));
		}
		/**
		 * Добавляет новый вызов метода в очередь
		 */
		public function addTask( t:BaseApiTask ):void
		{
			//customTrace("ApiWrapper:addTask:");
			tasks.push( t );
			t.addEventListener( Event.COMPLETE , onTaskDone );
		}
		protected function delTask( t:BaseApiTask ):void
		{
			var ind:int = tasks.indexOf( t );
			if ( ind != -1 )
			{
				tasks = tasks.splice( ind , 1 );
			}
		}
		protected function onTaskDone( e:Event ):void
		{
			var t:BaseApiTask = e.target as BaseApiTask;
			delTask( t );
		}
		
		public function preparePostToWall( ):IApiTask {
			throw(new Error("Error in ApiWrapper:preparePostToWall. "));
		}
		
		public function runTask( t:BaseApiTask ):Boolean
		{
			if ( tasks.indexOf( t ) == -1 )
			{
				return false;
			}
			return true;
		}
		
		public function getUID():String
		{
			return null;
		}
		public function getNetworkId():int
		{
			return 0;
		}
	}
}